package cn.dglydrpy.study.spring.ioc.xml;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/*
 * @Description:Spring单例bean用xml进行生命周期管理 
 * @author:dglydrpy
 * @date:2019年4月16日 下午11:35:51 
 * @version:1.0
 * Spring IOC:用于管理(创建,修改属性等)javaBean对象
 * javaBean规范:有包  实现序列化  getter()&setter() 重写SHE 属性私有化,行为公开化
 * toString()可根据实际用途任意重写
 */
public class LifeCycleBean implements Serializable,BeanNameAware,BeanFactoryAware,ApplicationContextAware,
InitializingBean,DisposableBean{
	
	private static final long serialVersionUID = 1L;
	
	private String name;
	private int age;
	private boolean alive;
	
	public LifeCycleBean() {
		this.alive = true;
		System.out.println("lifeCycleBean 构造方法实例化:"+this.toString());
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean isAlive() {
		return alive;
	}
	public void setAlive(boolean alive) {
		this.alive = alive;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + (alive ? 1231 : 1237);
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LifeCycleBean other = (LifeCycleBean) obj;
		if (age != other.age)
			return false;
		if (alive != other.alive)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "LifeCycleBean [name=" + name + ", age=" + age + ", alive=" + alive + "]";
	}
	
	@Override
	public void setBeanName(String name) {
		System.out.println("lifeCycleBean 注册我成为bean时定义的id: " + name);
	}
	
	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("lifeCycleBean 管理我的beanFactory为: " + beanFactory);
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("lifeCycleBean 高级容器接口applicationContext: " + applicationContext);
	}
	
	@PostConstruct
	public void postConstruct() {
		System.out.println("lifeCycleBean 执行@PostConstruct");
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("lifeCycleBean 执行InitializingBean.afterPropertiesSet()");  //在init()前执行
	}
	
	public void initMethod() {//先走配置文件的注入赋值,再走初始化方法
		System.out.println("lifeCycleBean 初始化方法initMethod:"+this.toString());
	}
	
	@Override
	public void destroy() throws Exception {
		System.out.println("lifeCycleBean 执行DisposableBean.destroy()");
	}
	
	public void destroyMethod() {
		this.alive = false;
		this.age = 100;
		System.out.println("lifeCycleBean 销毁方法destroyMethod:"+this.toString());
	}
	
	@PreDestroy
	public void preDestroy() {
		System.out.println("lifeCycleBean 执行@PreDestroy");
	}
}
